<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    :before-close="handleCancel"
    center
    width="800px"
    :append-to-body="true"
    :modal-append-to-body="false"
  >
    <el-form ref="editFormRef" :model="editForm" :rules="rules" size="small">
      <el-row>
        <el-col :span="12">
          <el-form-item label="选择角色" :label-width="labelWidth">
            <el-select v-model="editForm.roleNo" placeholder="请选择角色" @change="HandleRoleChange">
              <el-option
                v-for="item in roleList"
                :key="item.roleNo"
                :label="item.roleName"
                :value="item.roleNo"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <section class="user-box">
      <el-scrollbar style="height:100%">
        <el-table border :data="userList" style="width: 100%">
          <el-table-column prop="userNo" label="人员编号" width="188px"></el-table-column>
          <el-table-column prop="userName" label="人员名称"></el-table-column>
          <el-table-column prop="finishCount" label="已处理工单"></el-table-column>
          <el-table-column prop="handlingCount" label="待处理工单"></el-table-column>
          <el-table-column label="操作" width="100px">
            <template slot-scope="scope">
              <el-button @click="handleDispatch(scope.row)" type="text" size="small">派单</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-scrollbar>
    </section>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCancel" size="small">关 闭</el-button>
    </div>
  </el-dialog>
</template>

<script>
import Vue from 'vue'
import { getAllRoleList, getListByRoleNoList, workOrderDistribute } from '../js/api'

// import pick from 'lodash.pick'
import { USER_INFO } from '@/views/children/workOrder/js/constants'
export default {
  name: 'RoleModal',
  data () {
    return {
      tenantNo: Vue.ls.get(USER_INFO).tenantNo,
      title: '派单',
      isAdd: false,
      disabled: false,
      dialogVisible: false,
      labelWidth: '80px',
      editForm: {
        roleNo: ''
      },
      model: {},
      rules: {},
      roleList: [],
      userList: []
    }
  },
  computed: {},
  filters: {},
  mounted () {},
  methods: {
    async getAllRoleList () {
      let res = await getAllRoleList()
      this.roleList = res.data
    },
    HandleRoleChange (val) {
      this.getListByRoleNoList(val)
    },
    async getListByRoleNoList (roleNo) {
      let res = await getListByRoleNoList(roleNo)
      this.userList = res.data
    },
    // 编辑
    dispatch (record) {
      this.dialogVisible = true
      this.model = Object.assign({}, record)
      this.$nextTick(() => {
        this.getAllRoleList()
      })
    },
    // 派单
    handleDispatch (record) {
      console.log(record)
      this.$confirm('您确定派单给当前用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.dispatchFun(record)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消派单'
          })
        })
    },
    async dispatchFun (record) {
      const params = {
        distributeUser: record.userNo,
        no: this.model.no
      }
      const data = Object.assign({}, this.queryParams, params)
      let res = await workOrderDistribute(data)
      if (res.code === 0) {
        this.$message.success('派单成功!')
        let userInfo = {
          userNo: record.userNo,
          userName: record.userName,
          status: 200
        }
        this.$emit('ok', userInfo)
        this.handleCancel()
      }
    },
    handleDeviceChange (val) {
      this.getFloorData(val)
      this.editForm.roomFloor = ''
    },
    handleCancel () {
      this.model = {}
      this.editForm.roleNo = ''
      this.userList = []
      this.dialogVisible = false
      console.log('222222222')
      this.$refs.editFormRef.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
.dialog-footer {
  text-align: right;
}
.el-dialog {
  .el-input,
  .el-select {
    width: 100%;
  }
}
/deep/ .el-dialog__body {
  padding-bottom: 0;
}

.user-box {
  box-sizing: border-box;
  height: 400px;
  /deep/ .el-scrollbar__wrap {
    overflow-x: hidden;
  }
  .no-have {
    width: 100%;
    text-align: center;
    padding-top: 130px;
  }
}
</style>
